//
//  ZFBadgeAnimationView.m
//  ZFBadgeView
//
//  Created by fyzq on 2021/9/7.
//

#import "ZFBadgeAnimationView.h"
@interface ZFBadgeAnimationView()
@property(nonatomic,strong)CAAnimationGroup *groups;
@end
@implementation ZFBadgeAnimationView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initZFBadgeTipView];
    }
    return self;
}

-(void)initZFBadgeTipView{
    _color = [UIColor orangeColor];
    _scale = 2;
    _size = CGSizeMake(6, 6);
    _position = CGPointMake(5, 5);
}


- (void)layoutSubviews{
    [super layoutSubviews];

}



-(void)reloadData{
    [self setNeedsLayout];
}


- (void)drawRect:(CGRect)rect{
    
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);
    CGPoint center = CGPointMake(width/2, height/2);
    
    CALayer *layer = [[CALayer alloc] init];
    CABasicAnimation * scaleAnim = [CABasicAnimation animation];
    scaleAnim.keyPath = @"transform.scale";
    scaleAnim.fromValue = @0.3;
    scaleAnim.toValue = @1;
    CABasicAnimation *opacityAnim=[CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnim.fromValue= @1;
    opacityAnim.toValue= @0;
    
    CAAnimationGroup *group =[CAAnimationGroup animation];
    group.animations = @[scaleAnim,opacityAnim];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.duration = 2;
    group.repeatCount = FLT_MAX;
    
    layer.bounds = CGRectMake(0, 0, width*self.scale, height*self.scale);
    layer.backgroundColor = self.color.CGColor;
    layer.cornerRadius = CGRectGetWidth(layer.bounds)/2;
    layer.position = center;
    layer.masksToBounds = YES;
    [layer addAnimation:group forKey:@""];
  
    [self.layer addSublayer:layer];
    
    
    CALayer *bgLayer = [[CALayer  alloc] init];
    bgLayer.backgroundColor = self.color.CGColor;
    bgLayer.bounds = rect;
    bgLayer.cornerRadius = width/2;
    bgLayer.masksToBounds = YES;
    bgLayer.position = center;
    [self.layer addSublayer:bgLayer];

    
    CABasicAnimation *basic = [CABasicAnimation animation];
    basic.keyPath = @"";
    basic.fromValue = @0.8;
    basic.toValue = @1;
    basic.duration = 2;
    [bgLayer addAnimation:basic forKey:@""];
    
    
}

@end
